<template>
  <div class="goods-hot">
    <h3>{{ getTitle(type) }}</h3>
    <!-- 商品区块 -->

    <RouterLink
      to="/"
      class="goods-item"
      v-for="good in hotData"
      :key="good.id"
    >
      <img :src="good.picture" alt="" />
      <p class="name ellipsis">{{ good.name }}</p>
      <p class="desc ellipsis">{{ good.desc }}</p>
      <p class="price">&yen;{{ good.price }}</p>
    </RouterLink>
  </div>
</template>
<script>
import { ref } from 'vue'
import { useRoute } from 'vue-router'
// 思考：如何让组件在复用的时候可以支持多个热榜类型数据的渲染
// 1. title需要适配  2.数据列表
import { findHotGoods } from '@/api/good'

export default {
  name: 'GoodsHot',
  props: {
    type: {
      type: Number // 1.24小时 2.周  3.总
    }
  },
  setup (props) {
    // 枚举
    const TYPES = {
      1: '24小时热榜',
      2: '周热榜',
      3: '总热榜'
    }
    // 计算属性的小技巧
    // const title = computed(() => {
    //   return TYPES[props.type]
    // })

    function getTitle (type) {
      return TYPES[type]
    }

    // 获取数据
    const hotData = ref([])
    const route = useRoute()
    async function loadData () {
      const res = await findHotGoods({
        id: route.params.id,
        type: props.type
      })
      hotData.value = res.data.result
    }
    loadData()
    return {
      hotData,
      getTitle
    }
  }
}
</script>
<style scoped lang="less">
.goods-hot {
  h3 {
    height: 70px;
    background: @helpColor;
    color: #fff;
    font-size: 18px;
    line-height: 70px;
    padding-left: 25px;
    margin-bottom: 10px;
    font-weight: normal;
  }
  .goods-item {
    display: block;
    padding: 20px 30px;
    text-align: center;
    background: #fff;
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
}
</style>
